import 'package:flutter/material.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:dio/dio.dart';
import 'package:qgchat/page/live/dynamic/index.dart';

class Localtycoon extends StatefulWidget {
  Localtycoon({Key key}) : super(key: key);

  @override
  _LocaltycoonState createState() => _LocaltycoonState();
}

class _LocaltycoonState extends State<Localtycoon>
    with SingleTickerProviderStateMixin {
  TabController tabController; //tabbar控制器

  @override
  void initState() {
    super.initState();
    tabController = TabController(vsync: this, length: 3);
    // initRequest();
  }
  // Map<String,List<Map<String,Dynamic>>> rankList;
  Map<String,List> rankList = {"week":[{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/f4be9e0badcf6aef6b52f3300e73f589","id":7,"member_tel":"13777777777","member_token":"6a82b8a1f48021aab8aa81a236a86cc9","member_nickname":"茉莉","anchor_mobile":"","anchor_tag":"","member_createtime":"1577365608","anchor_workers":null,"gold":"6354","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/92764edee0296a16b10067b59ae74835","id":24,"member_tel":"15015800574","member_token":"03bb09cbe8a5d01cc76a8a6f634e7be2","member_nickname":"kaaya","anchor_mobile":"","anchor_tag":"","member_createtime":"1583808633","anchor_workers":null,"gold":"6000","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/18999437587f8205ef2945199076ce88","id":4,"member_tel":"13666666666","member_token":"09b3e5d8bc4c5090b5a1fabb0c9700e0","member_nickname":"模特小星星","anchor_mobile":"","anchor_tag":"","member_createtime":"1577342542","anchor_workers":null,"gold":"3552","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/JiaeMlEKcF9u3hibrfnsNyiaFDxJDH01cZXx9HxxzHIg86MDd5ia0o6jhHynwDrdTmU6AJP6h41QZK9QxlcpVezzzQ\/132","id":8,"member_tel":"13888888888","member_token":"8d84caa40b0786a50c0e88fbdc073c3b","member_nickname":"Lily","anchor_mobile":"","anchor_tag":"","member_createtime":"1577366050","anchor_workers":null,"gold":"1998","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/DibAj5nQus6pFb4wCuz1hoqFaINibdcMeUY0wgj2FudHbwBtKJzHML8jtwhEDpO4OCicibstQxOyf3STJTvldTFnkQ\/132","id":1045,"member_tel":null,"member_token":"4d28296b937d81c2d1c5841d2f108e8f","member_nickname":"じ柒柒ᝰ","anchor_mobile":"","anchor_tag":"","member_createtime":"1589047465","anchor_workers":null,"gold":"1080","is_online":false}],"month":[{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/f4be9e0badcf6aef6b52f3300e73f589","id":7,"member_tel":"13777777777","member_token":"6a82b8a1f48021aab8aa81a236a86cc9","member_nickname":"茉莉","anchor_mobile":"","anchor_tag":"","member_createtime":"1577365608","anchor_workers":null,"gold":"6354","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/92764edee0296a16b10067b59ae74835","id":24,"member_tel":"15015800574","member_token":"03bb09cbe8a5d01cc76a8a6f634e7be2","member_nickname":"kaaya","anchor_mobile":"","anchor_tag":"","member_createtime":"1583808633","anchor_workers":null,"gold":"6000","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/18999437587f8205ef2945199076ce88","id":4,"member_tel":"13666666666","member_token":"09b3e5d8bc4c5090b5a1fabb0c9700e0","member_nickname":"模特小星星","anchor_mobile":"","anchor_tag":"","member_createtime":"1577342542","anchor_workers":null,"gold":"3552","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/JiaeMlEKcF9u3hibrfnsNyiaFDxJDH01cZXx9HxxzHIg86MDd5ia0o6jhHynwDrdTmU6AJP6h41QZK9QxlcpVezzzQ\/132","id":8,"member_tel":"13888888888","member_token":"8d84caa40b0786a50c0e88fbdc073c3b","member_nickname":"Lily","anchor_mobile":"","anchor_tag":"","member_createtime":"1577366050","anchor_workers":null,"gold":"1998","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/DibAj5nQus6pFb4wCuz1hoqFaINibdcMeUY0wgj2FudHbwBtKJzHML8jtwhEDpO4OCicibstQxOyf3STJTvldTFnkQ\/132","id":1045,"member_tel":null,"member_token":"4d28296b937d81c2d1c5841d2f108e8f","member_nickname":"じ柒柒ᝰ","anchor_mobile":"","anchor_tag":"","member_createtime":"1589047465","anchor_workers":null,"gold":"1080","is_online":false}],"day":[{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/f4be9e0badcf6aef6b52f3300e73f589","id":7,"member_tel":"13777777777","member_token":"6a82b8a1f48021aab8aa81a236a86cc9","member_nickname":"茉莉","anchor_mobile":"","anchor_tag":"","member_createtime":"1577365608","anchor_workers":null,"gold":"6354","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/92764edee0296a16b10067b59ae74835","id":24,"member_tel":"15015800574","member_token":"03bb09cbe8a5d01cc76a8a6f634e7be2","member_nickname":"kaaya","anchor_mobile":"","anchor_tag":"","member_createtime":"1583808633","anchor_workers":null,"gold":"6000","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/cdn.duzhanjiaoyou.com\/18999437587f8205ef2945199076ce88","id":4,"member_tel":"13666666666","member_token":"09b3e5d8bc4c5090b5a1fabb0c9700e0","member_nickname":"模特小星星","anchor_mobile":"","anchor_tag":"","member_createtime":"1577342542","anchor_workers":null,"gold":"3552","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/JiaeMlEKcF9u3hibrfnsNyiaFDxJDH01cZXx9HxxzHIg86MDd5ia0o6jhHynwDrdTmU6AJP6h41QZK9QxlcpVezzzQ\/132","id":8,"member_tel":"13888888888","member_token":"8d84caa40b0786a50c0e88fbdc073c3b","member_nickname":"Lily","anchor_mobile":"","anchor_tag":"","member_createtime":"1577366050","anchor_workers":null,"gold":"1998","is_online":false},{"anchor_starts":3,"member_head":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/DibAj5nQus6pFb4wCuz1hoqFaINibdcMeUY0wgj2FudHbwBtKJzHML8jtwhEDpO4OCicibstQxOyf3STJTvldTFnkQ\/132","id":1045,"member_tel":null,"member_token":"4d28296b937d81c2d1c5841d2f108e8f","member_nickname":"じ柒柒ᝰ","anchor_mobile":"","anchor_tag":"","member_createtime":"1589047465","anchor_workers":null,"gold":"1080","is_online":false}]};
  initRequest() async {
    Dio dio = Dio();
    var res = await dio.get("http://192.168.1.33:8886/api/anchor/income");
    if (res.data["status"] == 1) {
      setState(() {
        rankList = Map<String,List>.from(res.data["data"]);
      });
    } else {
      toast(res.data["msg"]);
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        centerTitle: true,
        title: Container(
          width: px(260),
          padding: EdgeInsets.only(
            top: px(20),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(
                height: px(70),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: col('d25ed9'),
                      width: px(7),
                    ),
                  ),
                ),
                child: Text(
                  '魅力榜',
                  style: TextStyle(
                    fontSize: px(40),
                    color: col('222222'),
                  ),
                ),
              ),
              Container(
                height: px(70),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: col('d25ed9'),
                      width: px(7),
                    ),
                  ),
                ),
                child: Text(
                  '金主榜',
                  style: TextStyle(
                    fontSize: px(40),
                    color: col('222222'),
                  ),
                ),
              ),
            ],
          ),
        ),
        bottom: TabBar(
          controller: tabController,
          indicatorSize: TabBarIndicatorSize.label,
          indicatorColor: col('d25ed9'),
          labelColor: col('1f1f1f'),
          indicatorWeight: px(4),
          labelStyle: TextStyle(
            fontSize: px(32),
          ),
          unselectedLabelStyle: TextStyle(
            fontSize: px(32),
          ),
          unselectedLabelColor: col('bbbbbb'),
          tabs: <Widget>[
            Tab(
              text: '日榜',
            ),
            Tab(
              text: '周榜',
            ),
            Tab(
              text: '月榜',
            ),
          ],
        ),
      ),
      body: Container(
        child: TabBarView(
          controller: tabController,
          children: rankList != null ? <Widget>[
            LocalRank(list:rankList["day"]),
            LocalRank(list:rankList["week"]),
            LocalRank(list:rankList["month"]),
          ]
          :
          [Center(child: Text("加载中..."),),Center(child: Text("加载中..."),),Center(child: Text("加载中..."),)],
        ),
      ),
    );
  }
}

class LocalRank extends StatefulWidget {
  final List list;
  LocalRank({Key key,this.list}) : super(key: key);

  @override
  _LocalRankState createState() => _LocalRankState();
}

class _LocalRankState extends State<LocalRank> {
  bool isBiggerThan1 = false;
  bool isBiggerThan2 = false;
  bool isBiggerThan3 = false;
  bool isBiggerThan4 = false;
  @override
  void initState() { 
    super.initState();
    setState(() {//判断排行榜数量
      if (widget.list.length > 0) isBiggerThan1 = true;
      if (widget.list.length > 1) isBiggerThan2 = true;
      if (widget.list.length > 2) isBiggerThan3 = true;
      if (widget.list.length > 3) isBiggerThan4 = true;
    });
  }
  List<Widget> getStars(int anchorStarts) {//数字转星星
    List<Widget> res = [];
    for (int i = 0; i < anchorStarts; i++) {
      res.add(Image.asset("assets/images/al_score_star.png",width: px(25),));
      if ( i != anchorStarts - 1 ) {
        res.add(SizedBox(width: 3,));
      }
    }
    return res;
  }
  String getFollowerNum(String gold) {//粉丝数转换
    int hearts = int.parse(gold);
    if (hearts < 100000) {
      return hearts.toString();
    } else {
      double res = hearts/10000;
      return res.toStringAsFixed(1) + "w";
    }
  }
  List<Widget> getBelowThird() {//第四名及以后
    List<Widget> res = [];
    if (!isBiggerThan4) return res;
    List belowThirdList = widget.list.sublist(3);
    for (int i = 0; i < belowThirdList.length-1; i++) {
      res.add(
        Container(
          padding: EdgeInsets.symmetric(vertical: px(20)),
          margin: EdgeInsets.symmetric(horizontal: px(30)),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(width: px(2),color: col("eeeeee"))
            )
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Row(
                  children: <Widget>[
                    Text('${i+4}',style: TextStyle(color: col("434343"),fontSize: px(29),fontWeight: FontWeight.bold),),
                    SizedBox(width: px(24),),
                    Stack(
                      overflow: Overflow.visible,
                      children: <Widget>[
                        Container(
                          width: px(105),
                          height: px(105),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(50),
                            image: DecorationImage(
                              fit: BoxFit.contain,
                              image: NetworkImage(belowThirdList[i]["member_head"])
                            ),
                          ),
                        ),
                        belowThirdList[i]["is_online"]
                        ?
                        Positioned(
                          bottom: px(-16),
                          left: px(9),
                          child: Image.asset("assets/images/al_live_onair.png",width: px(87),height: px(31),)
                        )
                        :
                        SizedBox(height: 0,)
                      ],
                    ),
                    SizedBox(width: px(30),),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(belowThirdList[i]["member_nickname"]),
                        SizedBox(height: px(28),),
                        Row(
                          children: getStars(belowThirdList[i]["anchor_starts"])
                        )
                      ],
                    ),
                    
                  ],
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Text(getFollowerNum(belowThirdList[i]["gold"]),style: TextStyle(fontSize: px(21),color: col("9b9b9b")),),
                    SizedBox(width: px(10),),
                    Image.asset("assets/images/al_purple_heart.png",width: px(24),)
                  ],
                )
            ],
          ),
        )
      );
    }
    return res;
  }
  @override
  Widget build(BuildContext context) {
    return widget.list != null ? Container(
      child: Column(
        children: <Widget>[
          Container(
            height: px(780),
            child: Stack(
              overflow: Overflow.clip,
              children: <Widget>[
                Container(
            color: Colors.white,
            child: AspectRatio(
              aspectRatio: 750 / 650,
              // child: Image.asset("assets/images/al_award_table.png",
              child: Image.asset("assets/images/rank.png",
                  fit: BoxFit.fill),
            ),
          ),
          
          Positioned(
              //第一名
              left: px(297),
              top: px(87),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.circular(px(70)),
                    child:
                    isBiggerThan1
                    ?
                    Image.network(
                      widget.list[0]["member_head"],
                      width: px(136),
                      height: px(136),
                    )
                    :
                    SizedBox(height: 0,)
                  ),
                  SizedBox(height: px(14),),
                  Text(isBiggerThan1 ? widget.list[0]["member_nickname"] : "",style: TextStyle(fontSize: px(21),color: col("323232"),fontWeight: FontWeight.bold),),
                  SizedBox(height: px(14),),
                  Row(
                    children: isBiggerThan1 ? getStars(widget.list[0]["anchor_starts"]) : []
                  ),
                  SizedBox(height: px(14),),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text(isBiggerThan1 ? getFollowerNum(widget.list[0]["gold"]) : "",style: TextStyle(fontSize: px(21),color: col("0d0d0d")),),
                      SizedBox(width: px(10),),
                      Image.asset("assets/images/al_purple_heart.png",width: px(24),)
                    ],
                  )
                ],
              )),
              Positioned(
              //第二名
              left: px(63),
              top: px(156),
              child: Column(
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.circular(px(70)),
                    child:
                    isBiggerThan2
                    ?
                    Image.network(
                      widget.list[1]["member_head"],
                      width: px(136),
                      height: px(136),
                    )
                    :
                    SizedBox(height: 0,)
                  ),
                  SizedBox(height: px(14),),
                  Text(isBiggerThan2 ? widget.list[0]["member_nickname"] : "",style: TextStyle(fontSize: px(21),color: col("323232"),fontWeight: FontWeight.bold),),
                  SizedBox(height: px(14),),
                  Row(
                    children: isBiggerThan2 ? getStars(widget.list[1]["anchor_starts"]) : []
                  ),
                  SizedBox(height: px(10),),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text(isBiggerThan2 ? getFollowerNum(widget.list[1]["gold"]) : "",style: TextStyle(fontSize: px(21),color: col("0d0d0d")),),
                      SizedBox(width: px(10),),
                      Image.asset("assets/images/al_purple_heart.png",width: px(24),)
                    ],
                  )
                ],
              )),
              Positioned(
              //第三名
              left: px(533),
              top: px(178),
              child: Column(
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.circular(px(70)),
                    child:
                    isBiggerThan3
                    ?
                    Image.network(
                      widget.list[2]["member_head"],
                      width: px(136),
                      height: px(136),
                    )
                    :
                    SizedBox(height: 0,)
                  ),
                  SizedBox(height: px(14),),
                  Text(isBiggerThan3 ? widget.list[2]["member_nickname"] : "",style: TextStyle(fontSize: px(21),color: col("323232"),fontWeight: FontWeight.bold),),
                  SizedBox(height: px(14),),
                  Row(
                    children: isBiggerThan3 ? getStars(widget.list[0]["anchor_starts"]) : []
                  ),
                  SizedBox(height: px(14),),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text(isBiggerThan3 ? getFollowerNum(widget.list[2]["gold"]) : "",style: TextStyle(fontSize: px(21),color: col("0d0d0d")),),
                      SizedBox(width: px(10),),
                      Image.asset("assets/images/al_purple_heart.png",width: px(24),)
                    ],
                  )
                ],
              )
            ),
            // Positioned(//第一名皇冠
            //   left: px(290),
            //   top: px(52),
            //   child: Image.asset("assets/images/al_rank_no1.png",width: px(65),height: px(61),)
            // ),
            // Positioned(//第二名皇冠
            //   left: px(54),
            //   top: px(130),
            //   child: Image.asset("assets/images/al_rank_no2.png",width: px(60),height: px(54),)
            // ),
            // Positioned(//第三名皇冠
            //   right: px(60),
            //   top: px(140),
            //   child: Image.asset("assets/images/al_rank_no3.png",width: px(67),height: px(61),)
            // ),
              ],
            ),
          ),
          Expanded(
            child: Stack(
              overflow: Overflow.visible,
              children: <Widget>[
                Positioned(
                  top: px(-200),
                  left: 0,
                  child: Container(
                    padding: EdgeInsets.only(top: px(40)),
                    width: px(750),
                    height: px(720),
                    child: Column(
                      children: getBelowThird(),
                    ),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(
                          px(50),
                        ),
                        topRight: Radius.circular(
                          px(50),
                        ),
                      ),
                      boxShadow: [
                        BoxShadow(
                          offset: Offset(0.0, 0.0),
                          color: Color.fromRGBO(0, 0, 0, 0.42),
                          blurRadius: 25.0,
                        ),
                      ],
                    ),
                  ),
                )
              ],
            )
          )
        ],
      ),
    )
    :
    Center(child: Text("加载中..."),);
  }
}
